package com.vision.admin.cl.ui;

import com.google.gwt.core.client.GWT;
import com.sencha.gxt.widget.core.client.Component;
import com.sencha.gxt.widget.core.client.TabPanel;
import com.sencha.gxt.widget.core.client.container.CardLayoutContainer;
import com.vision.admin.activitylog.cl.ui.ActivityLogsPanel;
import com.vision.admin.cl.AdminTasks;
import com.vision.admin.company.cl.ui.CompaniesPanel;
import com.vision.admin.role.cl.ui.RolesPanel;
import com.vision.admin.user.cl.ui.MyProfilePanel;
import com.vision.admin.user.cl.ui.UsersPanel;
import com.vision.core.cl.CodeDownloadCallback;
import com.vision.core.cl.ui.ITabItemPanel;
import com.vision.core.cl.ui.ITabItemPanelBuilder;
import com.vision.core.cm.db.data.Company;

/**
 * Builder class for adding {@link ITabItemPanel} instances into a {@link TabPanel}. 
 * 
 * @author Mark
 *
 */
public class TabItemPanelBuilder implements ITabItemPanelBuilder {

	// Cached instances
	private ITabItemPanel dashboardPanel;
	private AbstractView<Company> companiesPanel;
	private ITabItemPanel usersPanel;
	private ITabItemPanel myProfilePanel;
	private ITabItemPanel rolesPanel;
	private ITabItemPanel activityLogsPanel;
	
	@Override
	public void create(String id, final Component parent) {
		// Companies
		if (AdminTasks.COMPANIES.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (companiesPanel == null)
						companiesPanel = new CompaniesPanel();
					activatePanel(parent, companiesPanel);
				}
			});
		}
		// My Profile
		else if (AdminTasks.PROFILE.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (myProfilePanel == null)
						myProfilePanel = new MyProfilePanel();
					activatePanel(parent, myProfilePanel);
				}
			});
		}
		// Roles
		else if (AdminTasks.ROLES.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (rolesPanel == null)
						rolesPanel = new RolesPanel();
					activatePanel(parent, rolesPanel);
				}
			});
		}
		// Activity Log
		else if (AdminTasks.ACTIVITY_LOG.getId().equals(id)) {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (activityLogsPanel == null)
						activityLogsPanel = new ActivityLogsPanel();
					activatePanel(parent, activityLogsPanel);
				}
			});
		}
//		else {
//			if (dashboardPanel == null)
//				dashboardPanel = new AdminDashboardPanel();
//			tabPanel.add(dashboardPanel, dashboardPanel.getConfig());
//			tabPanel.setActiveWidget(dashboardPanel);
//		}
		// Users
//		else if (AdminTasks.USERS.getId().equals(id)) {
		else {
			GWT.runAsync(new CodeDownloadCallback() {
				@Override
				public void onSuccess() {
					if (usersPanel == null)
						usersPanel = new UsersPanel();
					activatePanel(parent, usersPanel);
				}
			});
		}
	}
	
	protected void activatePanel(TabPanel tabPanel, ITabItemPanel panel) {
		tabPanel.add(panel, panel.getConfig());
		panel.loadData();
		tabPanel.setActiveWidget(panel);
	}
	
	// %%
	
	protected void activatePanel(CardLayoutContainer tabPanel, ITabItemPanel panel) {
		tabPanel.add(panel);
		panel.loadData();
		tabPanel.setActiveWidget(panel);
	}
	
	protected void activatePanel(Component tabPanel, ITabItemPanel panel) {
		if (tabPanel instanceof TabPanel)
			activatePanel((TabPanel) tabPanel, panel);
		else
			activatePanel((CardLayoutContainer) tabPanel, panel);
	}

}
